package com.xzavier.yunlian_interview.controller;

import com.xzavier.yunlian_interview.entity.User;
import com.xzavier.yunlian_interview.service.ClockResService;
import com.xzavier.yunlian_interview.service.ex.ClockResException;
import com.xzavier.yunlian_interview.utils.JsonResult;
import com.xzavier.yunlian_interview.view.ClockInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("interview/clockin")
public class ClockResController {
    @Autowired
    private ClockResService clockResService;

    @PostMapping("")
    public JsonResult<User> clockIn(HttpServletRequest request) {
//        String userid = (String) request.getAttribute("userid");
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new ClockResException("token缺失");
        }
        User user = clockResService.dailyClock(userId);
        return new JsonResult<User>(200, user);
    }

    @GetMapping("clockinInfo")
    public JsonResult<ClockInfo> getClockInfo(HttpServletRequest request, Integer year, Integer month) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new ClockResException("token缺失");
        }
        ClockInfo clockInfo = clockResService.dailyClockInfo(userId, year, month);
        return new JsonResult<ClockInfo>(200, clockInfo);

    }

}
